package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.Groupons;
import com.shijie.domain.entity.Products;
import com.shijie.domain.entity.Skus;
import com.shijie.domain.entity.UserGroupons;
import com.shijie.mapper.SkusMapper;
import com.shijie.mapper.UserGrouponsMapper;
import com.shijie.service.GrouponsService;
import com.shijie.service.ProductsService;

@RestController
@RequestMapping("/erp/groupons")
public class GrouponsController {

	@Resource
	private GrouponsService grouponsService;
	@Resource
	private UserGrouponsMapper userGrouponsMapper;
    @Resource
	private ProductsService productsService;
    @Resource
    private SkusMapper skusMapper;
	@GetMapping
	@RequiresPermissions(value={"groupons:list"})
	public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String productName,@RequestParam(required=false) Integer status) {
		PageHelper.startPage(page, pageSize);
		Condition condition = new Condition(Groupons.class);
		Example.Criteria c = condition.createCriteria();
		if(!StringUtils.isEmpty(productName)){
			c.andLike("productName", "%"+productName+"%");
		}
		if(status != null) {
			if(status == 0){
				c.andLessThanOrEqualTo("startDate", DateTime.now().toDate());
				c.andGreaterThanOrEqualTo("endDate", DateTime.now().toDate());
			}else{
				c.andEqualTo("status", status);	
			}
		}
		return ResultGenerator.success(grouponsService.selectPage(page, pageSize, condition));
	}
	
	@RequiresPermissions(value={"groupons:create"})
	@Log(title = "团购管理", action = BusinessType.INSERT)
	@PostMapping
	public Result add(@RequestBody Groupons groupons) {
		grouponsService.save(groupons);
		return ResultGenerator.success();
	}
	
	@RequiresPermissions(value={"coupons:edit"})
	@Log(title = "团购管理", action = BusinessType.UPDATE)
	@PutMapping
	public Result edit(@RequestBody Groupons groupons) {
		grouponsService.updateById(groupons);
		return ResultGenerator.success();
	}
	
	@RequiresPermissions(value={"groupons:delete"})
	@Log(title = "团购管理", action = BusinessType.UPDATE)
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable("id") Integer id) {
		grouponsService.deleteById(id);
		return ResultGenerator.success();
	}
	
	@GetMapping("/{id}")
	public Result get(@PathVariable("id") Integer id) {
		return ResultGenerator.success(grouponsService.selectById(id));
	}
	
	
	@GetMapping("/details/{id}")
	public Result details(@PathVariable("id") Integer id) {
		Condition condition = new Condition(UserGroupons.class);
		condition.createCriteria().andEqualTo("grouponId", id);
		return ResultGenerator.success(userGrouponsMapper.selectByCondition(condition));
	}
	
    @RequiresPermissions(value={"groupons:create","coupons:edit"})
	@GetMapping("products")
    public Result productList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String q
    		,@RequestParam(required=false) Integer status ,@RequestParam(required=false) Integer tradeType) {
        PageHelper.startPage(page, pageSize);
        List<Products> list = productsService.searchProducts(q,status,tradeType);
        list.forEach(c->{
        	Skus skus = new Skus();
        	skus.setProductId(c.getId());
        	c.setSkus(skusMapper.select(skus));
        });
        PageInfo<Products> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
	}
    
    @RequiresPermissions(value={"groupons:create","coupons:edit"})
	@GetMapping("/skus/{productId}")
    public Result productList(@PathVariable("productId") Integer productId) {
    	Skus sku = new Skus();
    	sku.setProductId(productId);
        return ResultGenerator.success(skusMapper.select(sku));
	}
}
